<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
		/*$wishlist = array(
			$wish  = array(

			    		'username' => '15计算机1班',
			    		'num'      => 'No.00001',
			    		'content'  => '今天天气不错',
			    		'time_at'  => '1489367050'
			    	),
			$wish  = array(

			    		'username' => '15计算机2班',
			    		'num'      => 'No.00002',
			    		'content'  => '今天天气不错',
			    		'time_at'  => '1489367050'
			    	),
			$wish  = array(

			    		'username' => '15计算机3班',
			    		'num'      => 'No.00003',
			    		'content'  => '今天天气不错',
			    		'time_at'  => '1489367050'
			    	)
		);
    	$this->assign('wishlist',$wishlist);*/
    	$db = M('yw');  // 用表名来实例化一个模型对
    	$wishlist = $db->select(); // select * from wishlist  
        p($wishlist);
	    $this->assign('wishlist',$wishlist);
        $this->display();


    }
    public function handler(){
		//$username = I('post.username') ;
        // $_POST['username']/  =左边是用于数据库的变字段名； =右边是表单的变量名
    	//$content  = I('post.content');
    	//$time_at  = time();  // 取当前时间戳
    	//$color    = rand(1,5);
    	//echo $username;
    	//echo "<br/>";
    	//echo $content;
    	//$db->add();  // 真正入库。
    	//echo $db->getLastSql();


    	/*$db = M('yw');   // 构建模型对象
    	$db->create();         // 标准实例，
    	$db->username = $username;
    	$db->content  = $content;
    	$db->time_at  = $time_at;
    	$db->color    = $color;
    	$db->add();  // 真正入库。
    	echo $db->getLastSql();*/

    	$data = array(
    		'username' =>  I('post.username','匿名'),
    		'content'  =>  I('post.content','这家伙很懒，什么都没留下'),
    		'time_at'  =>  time(),
    		'color'    =>  rand(1,5)
    	);
    	$db = M('yw');
    	dump($db->create($data));
    	//$db->add();  // 真正入库。
    	//echo $db->getLastSql();


    	
    	$result = $db->add();  // 真正入库。
    	// echo $db->getLastSql();
    	//dump($result);
    	if($result>0){
    		$this->success('操作完成','Index/index',3);
    	}else {
    		$this->error('新增失败');
    	}
    }
     public function test(){

        /*$username = I('post.username','匿名');
        if($username == ''){
            // $data['info'] = "不能为空！";
            // $data['status'] = -1;
            // $data['url'] = '';
        $this->error($data);}*/

        $data = array(
            'username' => I('post.username'),
            'content' => I('post.content'),
            'time_at' => time(),
            'color' => rand(1,5)
            );
        //$db = M('yw');
        //$db ->create($data);
        //$id = $db->add();
        $db = D('wish');
        $validate = $db->create($data);
        if(!$validate){
            $this->ajaxReturn($db->getError());
        }
            $id = $db->add();
            $data['id']  = $id;
            $data['time_at'] = date('Y-m-d H:i',$vo['time_at']);
            $data['content'] = phiz_replace($data['content']);
            $data['status'] = 1;
            
            //echo json_encode($data);
             $this->ajaxReturn($data);
        
        
    }
}